#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"


HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    //实例化界面
    setWig = new SetWig;
    stm32Wig = new Stm32Wig;
    monitor = new Monitor;
    search = new Search;
    count = new Count;
    history = new History;

    //将界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(stm32Wig);
    ui->stackedWidget->addWidget(setWig);
    ui->stackedWidget->addWidget(monitor);
    ui->stackedWidget->addWidget(search);
    ui->stackedWidget->addWidget(count);
    ui->stackedWidget->addWidget(history);
    ui->stackedWidget->setStyleSheet("QStackedWidget { background-image: url('://pic/780.jpg'); }");


    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.1.60"));//王伟：192.168.1.87,我的：192.168.0.145
    mqtt->setPort(1883);
    mqtt->connectToHost();

    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     stm32Wig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     setWig, SLOT(dataUpSlots1(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     count, SLOT(dataUpSlots2(QByteArray)));
}

HomePage::~HomePage()
{
    delete ui;
}

